package com.cn.qgx428.lazydiary.ui.activity.subActivity

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import android.widget.TextView
import com.cn.qgx428.lazydiary.Presenter.PlanPresenter
import com.cn.qgx428.lazydiary.Presenter.contract.PlanContract
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.MConst
import com.cn.qgx428.lazydiary.adapter.RAdapter
import com.cn.qgx428.lazydiary.adapter.RViewHolder
import com.cn.qgx428.lazydiary.extension.getCurrenDayOfYear
import com.cn.qgx428.lazydiary.network.Code
import com.cn.qgx428.lazydiary.network.bean.Plan
import com.cn.qgx428.lazydiary.network.bean.PlanClockIn
import com.cn.qgx428.lazydiary.network.bean.PlanDetail
import com.cn.qgx428.lazydiary.ui.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_plan_clock_in.*
import org.jetbrains.anko.toast
import java.util.*

class PlanClockInActivity : BaseActivity() {

    var planId = -1
    var time: String = ""
    var adpater = ItemAdapter(this, R.layout.adapter_plan_clock_in_re)
    var contents: MutableList<String> = mutableListOf()

    override fun initUI() {
        hindActionBar()
        setStatuBarColor(R.color.myGreenBlack)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_plan_clock_in)
        initView()
        checkClockIn()
        getContent()
    }

    private fun getContent() {
        PlanPresenter.getPlanDetail(planId, object: PlanContract.IPlanDetail{
            override fun onGetPlanDetalData(details: List<PlanDetail>) {
                details.forEach { contents.add(it.content) }
                adpater.setDatas(contents)
            }

            override fun onNoPlanDetailData() {
            }

        })
    }

    private fun initView() {
        planId = intent.extras.getInt(MConst.PLAN_ID)
        time = Calendar.getInstance().getCurrenDayOfYear()
        planClockIn_bar.setBackOnClickListener { finish() }
        planClockIn_commit.setOnClickListener { clockIn() }
        planClockIn_re.layoutManager = LinearLayoutManager(this)
        planClockIn_re.adapter = adpater
    }

    private fun checkClockIn() {
        PlanPresenter.checkClockIn(planId,time,{
            if (it.equals(Code.EXIST.toString())){
                planClockIn_line.visibility = View.GONE
                planClockIn_commit.visibility = View.GONE
                planClockIn_prump.visibility = View.VISIBLE
                planClockIn_remark.visibility = View.GONE
            }
        })

    }

    private fun clockIn(){
        PlanPresenter.clockIn(planId, contents, planClockIn_remark.text.toString(),time,{
            toast("打卡成功")
            finish()
        })
    }

    class ItemAdapter : RAdapter<String>{

        var sS: List<String> = arrayListOf()

        constructor(context: Context, layoutId: Int):super(context, layoutId)

        override fun conver(holder: RViewHolder, data: String) {
            val text = holder.getView<TextView>(R.id.adapter_plan_clockIn_text)
            text?.text = data
        }
    }
}
